package com.dandelion.common.utils;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;

public class ApiArgs {
    public static void legalUIntId(String name, Integer value) throws IllegalArgumentException {
        if (value == null || value <= 0) {
            throw new IllegalArgumentException("Parameter '" + name + "' is an illegal id value ");
        }
    }

    public static void legalIntId(String name, Integer value) throws IllegalArgumentException {
        if (value == null || value < 0) {
            throw new IllegalArgumentException("Parameter '" + name + "' is an illegal id value ");
        }
    }

    public static void legalLongId(String name, Long value) throws IllegalArgumentException {
        if (value == null || value <= 0L) {
            throw new IllegalArgumentException("Parameter '" + name + "' is an illegal id value ");
        }
    }

    public static String normalizeByteBool(String value) throws IllegalArgumentException {
        return "1".equals(value) ? "1" : "0";
    }

    public static <T> void notEmpty(String name, Collection<T> list) throws IllegalArgumentException {
        if (CollectionUtils.sizeIsEmpty(list)) {
            throw new IllegalArgumentException("Parameter '" + name + "' is empty");
        }
    }

    public static void notEmpty(String name, String value) throws IllegalArgumentException {
        if (StringUtils.isEmpty(value)) {
            throw new IllegalArgumentException("Parameter '" + name + "' is required");
        }
    }

    public static void notBlank(String name, String value) throws IllegalArgumentException {
        if (StringUtils.isBlank(value)) {
            throw new IllegalArgumentException("Parameter '" + name + "' must not be blank");
        }
    }

    public static void notBlankAndLenLessThan(String name, String value, int maxLen) {
        notBlank(name, value);
        lenLessThan(name, value, maxLen);
    }

    public static void lenLessThan(String name, String value, int maxLen) {
        if (value == null) {
            return;
        }
        if (maxLen > 0 && value.length() > maxLen) {
            throw new IllegalArgumentException("Parameter '" + name + "' length should < " + maxLen);
        }
    }

    public void notEmptyAndCheckLen(String name, String value, int maxLen) {
        notBlank(name, value);
        lenLessThan(name, value, maxLen);
    }
}
